﻿using System.Data;
using FastReport;
using HRMSystemShi.DAL;
using HRMSystemShi.Model;

namespace HRMSystemShi._2025
{
    public partial class FormSalarySheetReport : Form
    {
        public FormSalarySheetReport()
        {
            InitializeComponent();
        }
        private void SalarySheetReport_Load(object sender, EventArgs e)
        {

            comboBoxYear.DataSource = SalarySheetService.GetYearList();
            comboBoxMonth.Enabled = false;
            comboBoxDepartment.DataSource = DepartmentService.GetDepartmentList();
            comboBoxDepartment.DisplayMember = "Name";
            comboBoxDepartment.ValueMember = "Id";
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            int year = (int)(comboBoxYear.SelectedItem ?? DateTime.Now.Year);
            int month = (int)(comboBoxMonth.SelectedItem ?? DateTime.Now.Month);
            Guid departmentId = (Guid)(comboBoxDepartment.SelectedValue ?? Guid.Empty);
            SalarySheet salarySheet = SalarySheetService.GetSalarySheet(year, month, departmentId);
            if (salarySheet == null)
            {
                CommonHelper.MessageError("没有找到该工资单，请先创建工资单后重试");
                return;
            }
            else
            {
                Guid id = salarySheet.Id;
            }

            DataTable? dt = SalarySheetService.GetSalarySheetItems(salarySheet);
            if (dt != null)
            {
                Report report = new Report();
                report.Load("D:\\桌面\\HRMSystemShi.2025\\SalarySheetReport.frx");
                report.RegisterData(dt, "SalarySheetReport");
                report.SetParameterValue("dep", comboBoxDepartment.Text);
                report.SetParameterValue("month", month);
                report.SetParameterValue("year", year);

                if (report.Prepare())
                {
                    report.Preview = previewControlSalaryReport;
                    report.ShowPrepared();
                }
            }

        }

        private void comboBoxYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxMonth.Enabled = true;
            comboBoxMonth.DataSource = SalarySheetService.GetMonthList((int)comboBoxYear.SelectedValue!);
        }
    }
}
